#include <iostream>
#include <memory>

// -------------------- Subject 接口 --------------------

class Subject {
public:
    virtual void request() = 0;          // 纯虚函数，处理请求
    virtual ~Subject() {}
};

// -------------------- RealSubject 类 --------------------

// 真实对象，实现了 Subject 接口
class RealSubject : public Subject {
public:
    void request() override {
        std::cout << "RealSubject: Handling request." << std::endl;
    }
};

// -------------------- Proxy 类 --------------------

// 代理类，也实现了 Subject 接口
class Proxy : public Subject {
private:
    std::unique_ptr<RealSubject> realSubject; // 使用智能指针管理 RealSubject 对象

public:
    // 构造函数，初始化 RealSubject 对象
    Proxy() : realSubject(std::make_unique<RealSubject>()) {}

    // 实现 Subject 接口的方法
    void request() override {
        std::cout << "Proxy: Doing some work before passing request." << std::endl;
        realSubject->request();
        std::cout << "Proxy: Doing some work after passing request." << std::endl;
    }

    // 析构函数
    ~Proxy() {
        std::cout << "Proxy: Destroying Proxy and RealSubject." << std::endl;
    }
};

// -------------------- 测试用例 --------------------

int main() {
    // 创建一个代理对象
    Subject* proxy = new Proxy();

    // 通过代理对象发起请求
    std::cout << "---- Making a request through Proxy ----" << std::endl;
    proxy->request();

    // 释放内存
    delete proxy;

    return 0;
}
